package com.bookmanger.server.mapper;

import com.bookmanger.pojo.Entity.BookType;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BookTypeMapper {
    
    /**
     * 查询所有图书分类
     */
    @Select("SELECT id, type_name as typeName, parent_id as parentId, is_delete as isDelete FROM book_type")
    List<BookType> findAll();
    
    /**
     * 根据ID查询图书分类
     */
    @Select("SELECT id, type_name as typeName, parent_id as parentId, is_delete as isDelete FROM book_type WHERE id = #{id}")
    BookType findById(Integer id);
    
    /**
     * 插入新的图书分类
     */
    @Insert("INSERT INTO book_type(type_name, parent_id, is_delete) VALUES(#{typeName}, #{parentId}, #{isDelete})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(BookType bookType);
    
    /**
     * 更新图书分类
     */
    @Update("UPDATE book_type SET type_name = #{typeName}, parent_id = #{parentId} WHERE id = #{id}")
    int update(BookType bookType);
    
    /**
     * 查询所有未删除的图书分类
     */
    @Select("SELECT id, type_name as typeName, parent_id as parentId, is_delete as isDelete FROM book_type WHERE is_delete = 0")
    List<BookType> findAllNotDeleted();
    
    /**
     * 根据父ID查询子分类
     */
    @Select("SELECT id, type_name as typeName, parent_id as parentId, is_delete as isDelete FROM book_type WHERE parent_id = #{parentId} AND is_delete = 0")
    List<BookType> findByParentId(Integer parentId);
    
    /**
     * 假删图书分类（设置is_delete为1）
     */
    @Update("UPDATE book_type SET is_delete = 1 WHERE id = #{id}")
    int softDeleteById(Integer id);
}